<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head>
    <meta charset="utf-8">
    <title>分类添加</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/layuimini/lib/layui-v2.6.3/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/layuimini/css/public.css}" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
    <div class="layui-form layuimini-form">
        <input id="id" type="hidden" name="id" th:value="${resource.id}">
        <div class="layui-form-item">
            <label class="layui-form-label required">资源名</label>
            <div class="layui-input-block">
                <input type="text" name="name" th:value="${resource.name}" lay-verify="required" lay-reqtext="资源名不能为空" placeholder="请输入资源名" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label required">一级分类</label>
            <div class="layui-col-md3 layui-show-xs-block">
                <div id="oneType" class="xm-select-demo"></div>
            </div>

            <label class="layui-form-label required">二级分类</label>
            <div class="layui-col-md4 layui-show-xs-block">
                <div id="twoType" class="xm-select-demo"></div>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label required">介绍</label>
            <div class="layui-input-block">
                <input type="text" name="description" th:value="${resource.description}" lay-verify="required|description" lay-reqtext="介绍不能为空" placeholder="请输入介绍(最多输入15个字)" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label required">链接</label>
            <div class="layui-input-block">
                <input type="text" name="link" th:value="${resource.link}" lay-verify="required" lay-reqtext="链接不能为空" placeholder="请输入链接" value="" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label required">资源封面</label>
            <div class="layui-upload">
                <button type="button" class="layui-btn" id="selectImg">选择图片</button>
                <button type="button" class="layui-btn" id="uploadImg">上传图片</button>
                <input id="cover" type="hidden" name="cover" lay-verify="required" lay-reqtext="请选择封面好然后点击上传图片" class="layui-input">
                <div class="layui-upload-list">
                    <img th:src="${resource.cover}" style="width: 92px; height: 92px; border-radius: 50%; margin-left: 12.5%" id="pic">
                </div>
                <div style="width: 95px; margin-left: 12.5%">
                    <div class="layui-progress layui-progress-big" lay-showpercent="yes" lay-filter="progress">
                        <div class="layui-progress-bar" lay-percent=""></div>
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认添加</button>
            </div>
        </div>
    </div>

<script th:src="@{/layuimini/lib/layui-v2.6.3/layui.js}" charset="utf-8"></script>
<script th:src="@{/layuimini/lib/jquery-3.4.1/jquery-3.4.1.min.js}"></script>
<script th:src="@{/layuimini/js/lay-config.js}" charset="utf-8"></script>
<script>
    layui.use(["jquery", "form", "layer", "xmSelect"], function () {
        var $ = layui.jquery,
            layer = layui.layer,
            form = layui.form,
            xmSelect = layui.xmSelect,
            element = layui.element,
            upload = layui.upload;

        upload.render({
            elem: '#selectImg',
            url: '/admin/upload/uploadResourceCover', //此处用的是第三方的 http 请求演示，实际使用时改成您自己的上传接口即可。
            auto: false,
            bindAction: '#uploadImg',
            choose: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#pic').attr('src', result); //图片链接（base64）
                });
            },
            done: function(res){
                //上传成功的一些操作
                if (res.code === 200) {
                    $('#cover').val(res.data.url)
                } else if (res.code === 444) {
                    $('#picText').html(''); //置空上传失败的状态

                }
            },
            //进度条
            progress: function(n, elem, e){
                element.progress('progress', n + '%'); //可配合 layui 进度条元素使用
                if(n == 100){
                    layer.msg('上传完毕', {icon: 1});
                }
            }
        });

        //获取ID
        var id = $('#id').val();

        //一级分类列表
        $.post('/admin/resource-type/selectParentNames', function (res) {
            var oneTypeData = res.data.resourceTypes
            var oneType = xmSelect.render({
                el: '#oneType',
                tips: '请选择一级分类',
                name: 'oneTypeId',
                radio: true,
                clickClose: true,
                model: {
                    label: {
                        type: 'text',
                        text: {
                            //左边拼接的字符
                            left: '',
                            //右边拼接的字符
                            right: '',
                            //中间的分隔符
                            separator: ', ',
                        },
                    }
                },
                layVerify: 'required',
                layReqText: '请选择一级分类',
                data: oneTypeData,
                on: function (data) {
                    //arr:  当前多选已选中的数据
                    var arr = data.arr
                    var id = arr[0].value;

                    $.get('/admin/resource-type/selectTwoTypes/' + id, function (res) {
                        xmSelect.render({
                            el: '#twoType',
                            tips: '请选择二级分类',
                            name: 'twoTypeId',
                            radio: true,
                            clickClose: true,
                            model: {
                                label: {
                                    type: 'text',
                                    text: {
                                        //左边拼接的字符
                                        left: '',
                                        //右边拼接的字符
                                        right: '',
                                        //中间的分隔符
                                        separator: ', ',
                                    },
                                }
                            },
                            layVerify: 'required',
                            layReqText: '请选择二级分类',
                            data: res.data.twoTypes
                        })
                    })
                }
            })


            $.get('/admin/resource/selectOneChecked/' + id, function (res) {
                oneType.setValue(res.data.oneChecked)
            })
        })

        $.get('/admin/resource/selectTwoTypeList/' + id, function (res) {
            //二级分类
            var twoType = xmSelect.render({
                el: '#twoType',
                tips: '请选择二级分类',
                name: 'twoTypeId',
                empty: '请先选择一级分类',
                radio: true,
                clickClose: true,
                model: {
                    label: {
                        type: 'text',
                        text: {
                            //左边拼接的字符
                            left: '',
                            //右边拼接的字符
                            right: '',
                            //中间的分隔符
                            separator: ', ',
                        },
                    }
                },
                layVerify: 'required',
                layReqText: '请选择二级分类',
                data: res.data.twoTypeList
            })

            $.get('/admin/resource/selectTwoChecked/' + id, function (res) {
               twoType.setValue(res.data.twoChecked)
            })
        })

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            data = data.field

            $.post('/admin/resource/editResource', data, function (res) {
                if (res.code === 200) {
                    layer.msg(res.msg, function () {
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                        //刷新父页面
                        window.parent.location.reload();
                    })
                } else if (res.code === 444) {
                    layer.msg(res.msg)
                }
            })

            return false;
        })

        form.verify({
            description: function (value, item) { //value：表单的值、item：表单的DOM对象
                if (value.length > 15) {
                    return '介绍不能超过100个字符';
                }
            }
        });
    });

</script>
</body>
</html>
